﻿<?xml version="1.0" encoding="utf-8" ?>
<AdoNetDataAccess connection="$ArticleDBConnection$" formatter="bizobj of Article(primaryPicture)">
	<FormatterInfo ArticleFieldType="columnName">
    <code for="vars" on="Article">
      bool stripLeading = true;
    </code>
    <code for="postEach" on="Article">
      <![CDATA[
			try
			{
        bool stripLeading = true;
        if(info.get("stripLeading") is bool)
          stripLeading = (bool)info.get("stripLeading");
        else if(info.get("stripLeading") is string)
          stripLeading = (string.Compare((string)info.get("stripLeading"), "False", true) == 0)?false:true;
        
        if(stripLeading)
        {
          string title = (string)CurrentBizObj.get("baseTitle");
          int titIndex = title.IndexOf("~!");
          if(titIndex != -1)
            CurrentBizObj.put("baseTitle", title.Substring(titIndex+2));
        }
			}
			catch(Exception e)
			{
			}
		]]>
    </code>
    <code for="bizObjType" on="Article">
			<![CDATA[
			obj = App.bizObjMgr.create(info.get("BizObjType") != null?info.get("BizObjType").ToString():"Article");
		]]>
		</code>
		<code for="bizObjListType" on="Article">
			<![CDATA[
			m_list = App.bizObjMgr.createList(info.get("BizObjType") != null?info.get("BizObjType").ToString():"Article");
		]]>
		</code>
	</FormatterInfo>
	<Sql>
		SELECT        
			a.id
			<code>
				<![CDATA[
			DetailLevel details = (DetailLevel)info.get("details");
			if(details > DetailLevel.idOnly)
			{
				sql.Append(",a.baseTitle,a.uniqueTitle");
				sql.Append(",t.objectId, t.parentId, t.parentHostId, nc.keyName, nc.depth, nc.rootId, nc.treeName");
			}
			if(details > DetailLevel.idAndTitle)
			{
				sql.Append(",a.readMoreJump,a.isSystem");
				sql.Append(",u.lastName AS counts_author_lastName, u.id AS counts_author_id, u.firstName AS counts_author_firstName, u.userName AS counts_author_username");
			}
			if(details > DetailLevel.idAndTitlePlus)
			{
				sql.Append(",a.summary AS baseSummary, a.keyword, a.publishAuthorName, a.publishDate, nc.dateUpdated as counts_dateUpdated, nc.dateCreated as counts_dateCreated,a.metaType as suggestedDisplayType");
				sql.Append(",a.id AS primaryPictureId,a.primaryMediaName AS primaryPictureName, a.primaryMediaName AS primaryPictureFileName, a.caption AS primaryPictureCaption");
				if(true.Equals(info.get("includeTranslations")))
					sql.Append(",a.translatedTitles,a.translatedSummaries,a.translationDetails,a.translatedCaptions");
			}
			
			if(details > DetailLevel.basicDetails)
			{
				if(true.Equals(info.get("includeBody")))
				{
					sql.Append(",a.body as baseBody");
					if(true.Equals(info.get("includeTranslations")))
						sql.Append(",a.translatedBodies");
				}
			}
			]]>
			</code>
			FROM Articles AS a 
			<code><![CDATA[
				sql.Append("LEFT OUTER JOIN ObjectTreeNodes AS t ON a.id = t.objectId LEFT OUTER JOIN ObjectTreeCounts AS nc on nc.objectId = a.id ");
				if(details > DetailLevel.idAndTitle)
					sql.Append("LEFT OUTER JOIN Users AS u ON u.id = nc.authorId ");
			]]></code>
			WHERE 
				a.companyId = @companyId AND (t.objectId = t.parentHostId) AND 
				<code><![CDATA[
					if(info.get("uniqueTitle")!=null)
						sql.Append(" a.uniqueTitle=@uniqueTitle ");
					else
						sql.Append(" a.id=@articleId ");
				]]>
				</code>
	</Sql>
	<Param name="articleId" type="UniqueIdentifier" />
	<Param name="uniqueTitle" type="UniCodeVarChar" />
	<Param name="companyId" type="UniqueIdentifier" />
</AdoNetDataAccess>
